<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 * $Id: 2017-12-13 03:39:35 apple $
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
if($_RQ['basicname']){
	$_SESSION['plugin_name']=$_RQ['basicname'];
}
$return=array();
if(!$_RQ['plugin']){
	exi('请先选择模块！','error');
}
//访问后台插件页面，判断套餐：
$groupid=pdo_getcolumn('core_account_group',array('id'=>$_SESSION['account']['groupid']),'id');
if($groupid && $_SESSION['this']=='manage' && !$_SESSION['isfounder'] && $_SESSION['account']['endtime'] && $_SESSION['account']['endtime']<TIMESTAMP){
			exi('您的套餐已经到期!请及时续费！','error','core/account.siteinfo/detail/id='.$_SESSION['account']['groupid']);
}
$return['m']=$_RQ['plugin'];
if($_RQ['op']=='list'){
	if($_RQ['plugin']!='core'){
		$module=pdo_get('core_plugins',array('name'=>$_RQ['plugin']));
		if(!$module){
			exi('找不到指定模块！'.$_RQ['plugin'],'error');
		}
	}else{
		$return['child_action']=$_RQ['child_action']?:'account.welcome';
		$htmls[]=array('type'=>'list_framess');
		$return['htmls']=$htmls;
		exi($return);
	}
	if($module['is_legao'] && $_SESSION['os']=='windows'){
		//乐高体直接进入插件首页
		$_SESSION['current_plugin']=$module['name'];
		exi('','',$module['name'].'/index/');
	}elseif($_RQ['current_plugin']){
		$_SESSION['current_plugin']='core';
	}
	if($module['type']==4){
		$module['setting']=pdo_getcolumn('core_old_plugins',array('name'=>$module['name']),'setting');
	}
	//检查插件是否自定义导航菜单，如果是。抓取菜单第一个页面作为插件首页
	/*$index_file=ROOT_PLUGIN.$module['name'].'/manage/actions/index.php';
	if(is_file($index_file)){
		exi('','',$module['name'].'/index//basicname='.$_RQ['basicname']);
	}*/
	
	$covers=$module['covers']?json_decode($module['covers'],true):array();
	if($covers){
		foreach($covers as &$cover){
			$cover['url']=array('plugin'=>$module['name'],'action'=>'basic.module_home','op'=>'cover','query'=>'ac='.$cover['name'].'&type='.$module['type']);
			unset($cover['name']);
		}
	}
	if($module['setting']){
		if($module['type']!=4){
			$covers[]=array('title'=>'参数设置','url'=>array('action'=>'setting','plugin'=>$module['name']));
		}else{
			$covers[]=array('title'=>'参数设置','url'=>'./index.php?action=module&do=setting&m='.$module['name']);
		}
	}
	$menus=json_decode($module['menus'],true);
	if($menus){
		if($module['type']!=4){
			foreach($menus as &$menu){
				$menu['url']=array('action'=>$menu['name'],'plugin'=>$module['name']);
				unset($menu['name']);
			}
		}else{
			foreach($menus as &$menu){
				$menu['url']='./index.php?action=module&do='.$menu['name'].'&m='.$module['name'];
				unset($menu['name']);
			}
		}
		
	}
	$return['title']=$module['title'];
	if($module['base']){
		$module['base']=trim($module['base'],';');
		$module['base']=explode(';',$module['base']);
		$bases=array();
		foreach($module['base'] as $plugin){
			$plugin=pdo_get('core_plugins',array('name'=>$plugin),array('name','title'));
			$bases[]=array('title'=>$plugin['title'],'url'=>array('action'=>'module_home','plugin'=>$plugin['name']),'image'=>$_AR['siteroot'].'addons/'.$plugin['name'].'/icon.jpg');
		}
	}
	$htmls=array();
	$menu_list=array();
	if($covers){
		$htmls[]=array('type'=>'list_menu','title'=>'基础功能设置','menus'=>$covers);
		$menu_list[]=array('title'=>'基础功能设置','child'=>$covers);
	}
	if($menus){
		$htmls[]=array('type'=>'list_menu','title'=>'插件业务菜单','menus'=>$menus);
		$menu_list[]=array('title'=>'插件业务菜单','child'=>$menus);
	}
	if($module['type']!=4){
		//非兼容插件，检查插件是否有设置菜单
		$menu_file=ROOT_PLUGIN.$module['name'].'/model/menu.php';
		if(is_file($menu_file)){
			$menu_list=plugin($module['name'])->menu();
			$htmls=array();
			if($_SESSION['os']!='windows'){
				$htmls[]=array('type'=>'module_top');
				if($module['is_legao']){
					$_SESSION['manage_plugin']=$module['name'];
					$htmls[]=array('type'=>'list_menu3','menus'=>$menu_list);
				}else{
					$htmls[]=array('type'=>'list_menu2','menus'=>$menu_list);
				}
			}else{
				foreach($menu_list as $menu){
					$htmls[]=array('type'=>'list_menu','title'=>$menu['title'],'menus'=>$menu['child']);
				}
			}
		}
		$url=custom_url($module['name'].'/index/',true);
		$return['qrcode']=cfc('tools')->createqrcode($url,'250',true);
		$return['qrcode']=tomedia($return['qrcode']);
	}
	if($menu_list){
		set_html('top_plugin_menu',$menu_list);
	}
	if($module['hide']){
		if(!$htmls['0']){
			exi('该插件没有东西可以展示，请先设置插件功能！','error','goback');
		}
		$tabs=array(
			array('title'=>'返回插件开发工具','plugin'=>'core','action'=>'app_develop.plugin','op'=>'edit')
		);
		set_html('tabs',$tabs);
	}
	if($_SESSION['os']!='windows'){
		if($module['welcomehdps']){
			$module['welcomehdps']=json_decode($module['welcomehdps'],true);
			$return['iswelcome']=count($module['welcomehdps'])>0?1:0;
		}
		if(($module['name']=='jy_weishop' || $module['basic']=='jy_weishop')&&$return['iswelcome']!=1){
				$return['iswelcome']=1;
		}
		if($return['iswelcome']==1 && $_SESSION['iswelcome']!=1){
			exi('','','core/basic.module_home/welcome/pluginname='.$module['name']);
		}
		$return['iswelcome']='';
		$return['plugin']= $module;
		$return['account']=pdo_get('core_accounts',array('uniacid'=>$_SESSION['uniacid']),array('uniacid','title','desc','logo','groupid','credit2','endtime','opendiys'));
		$return['group']=pdo_get('core_account_group',array('id'=>$return['account']['groupid']),array('id','title','access','resource','opendiys'));
		if($return['account']['endtime'] && $return['account']['endtime']<time()){
			$return['group']['status']=2;
		}elseif($return['account']['endtime']>time()){
			$return['group']['status']=1;
		}elseif(!$return['account']['endtime']){
			$return['group']['status']=1;
		}
		$return['account']['endtime']=$return['account']['endtime']?date('Y-m-d',$return['account']['endtime']):'长期有效';
		$return['store']['logo']=$return['store']['logo']?$return['store']['logo']:$avatar;
		$return['status0']=pdo_count('jy_weishop_order',array('uniacid'=>$_SESSION['uniacid'],'status'=>0,'createtime >'=>$daytime));
		$return['status1']=pdo_count('jy_weishop_order',array('uniacid'=>$_SESSION['uniacid'],'status'=>1,'createtime >'=>$daytime));
		$return['status2']=pdo_count('jy_weishop_order',array('uniacid'=>$_SESSION['uniacid'],'status'=>2,'createtime >'=>$daytime));
		$return['status3']=pdo_count('jy_weishop_order',array('uniacid'=>$_SESSION['uniacid'],'status'=>3,'createtime >'=>$daytime));
		$return['status4']=pdo_count('jy_weishop_order',array('uniacid'=>$_SESSION['uniacid'],'status'=>4,'createtime >'=>$daytime));
		$return['ispay']=pdo_count('jy_weishop_order',array('uniacid'=>$_SESSION['uniacid'],'ispay'=>1,'createtime >'=>$daytime),'id');
		$return['ispayfee']=pdo_getsum('jy_weishop_order',array('uniacid'=>$_SESSION['uniacid'],'ispay'=>1,'createtime >'=>$daytime),'price');
		$return['orderfee']=price($return['ispayfee']/$return['ispay']);
		$return['looks'] = pdo_count('core_access_log',array('uniacid'=>$_SESSION['uniacid'],'plugin'=>$module['name'],'createtime >'=>$daytime));
	}else{
		if($module['type']!=4){
			if(is_file(ROOT_PLUGIN.$module['name'].'/manage/actions/index.php')){
				exi('','',$module['name'].'/index/');
			}
		}
	}
	$sharelink=$module['name'].'/index/';
	$pluginset=plugin($module['name'])->plugin_setting('basic');
	if($module['name']=='jy_weishop'){
		$pluginset=plugin('jy_weishop')->plugin_setting('setting');
	}
	$return['sharedata']=array(
			'title'=>$pluginset['share_title']?:$module['title'],
			'desc'=>$pluginset['share_desc']?:$module['title'].',大家来围观一下！',
			'link'=>custom_url($sharelink,true),
			'imgUrl'=>$pluginset['share_image']?ATTACHMENT_URL.$pluginset['share_image']:$module['image']
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='get_menu'){
	if($_SESSION['current_plugin'] != 'core' && $_SESSION['current_plugin'] != $_RQ['plugin']){
		$plugin=pdo_get('core_plugins',array('name'=>$_SESSION['current_plugin']));
		$child_plugin=pdo_get('core_plugins',array('name'=>$_RQ['plugin']),array('name','title'));
	}else{
		$plugin=pdo_get('core_plugins',array('name'=>$_RQ['plugin']));
	}
	$return['title']=$plugin['title'];
	$return['name']=$plugin['name'];
	$return['child_plugin']=$child_plugin['name'];
	$return['child_title']=$child_plugin['title'];
	$return['current_plugin']=$_SESSION['current_plugin'];
	$return['image']=tomedia($plugin['image']);
	if($plugin['type']!=4){
		$return['menus']=json_decode($plugin['menus'],true);
	}
	exi($return);
}
if($_RQ['op']=='cover'){
	$action=trim($_RQ['ac']);
	$type=intval($_RQ['type']);
	$ismanage=intval($_RQ['ismanage']);
	if(!$action){
		exi('参数错误！','error');
	}
	$params=array('action'=>$action,'plugin'=>$_RQ['plugin']);
	$item=cfc('core')->get_back_newss($params);	
	//if(!$item){
		if($type!=4){
			//$is_legao=pdo_getcolumn('core_plugins',array('name'=>$_RQ['plugin']),'is_legao');
			//if($is_legao){
				$item['system_url']=$_RQ['plugin'].'/'.$action.'//current_plugin='.$_RQ['plugin'];
				if($ismanage){
					$item['url']=manage_url($item['system_url']);
					$item['curl']=manage_url($item['system_url'],true);
				}else{
					$item['url']=custom_url($item['system_url']);
					$item['curl']=custom_url($item['system_url'],true);
				}
				$item['manageurl']=manage_url($_RQ['plugin'].'/basic.module_home//current_plugin='.$_RQ['plugin'],true);
				$item['manageurl2']=manage_url($_RQ['plugin'].'/basic.module_home/',true);
			/*}else{
				$item['system_url']=$_RQ['plugin'].'/'.$action.'/';
				$item['url']=custom_url($item['system_url']);
				$item['curl']=custom_url($item['system_url'],true);
			}*/
			
		}else{
			//微赞兼容插件
			$item['system_url']='core/module//m='.$_RQ['plugin'].'&do='.$action;
			$item['url']=custom_url($item['system_url']);
			$item['curl']=custom_url($item['system_url'],true);
		}
	//}
	
	$item['action']=$action;
	$return['item']=$item;
	$return['keytypes']=array(
		'1'=>'精确匹配',
		'2'=>'模糊匹配',
		'3'=>'正则匹配'
	);
	$setting=core_setting('site');
	$return['htmls']=array(
		'type'=>'edit',
		'post'=>'post',
		'edits'=>array(
			array('title'=>'设置入口信息','list'=>array(
				array('type'=>'copy','name'=>'curl','title'=>'直接URL','read'=>true,'help'=>'用于浏览器直接访问的地址'),
				array('type'=>'copy','name'=>'system_url','title'=>'专用action链接','read'=>true,'help'=>'用于系统的链接地址；弥补上面<red>直接URL</red>，有时会在系统，底部菜单等地方无法使用！'),
				array('type'=>'ewm','name'=>'system_url','title'=>'二维码','show_wxapp_ewm'=>$setting['cover_wxapp']?false:true,'help'=>'如果小程序二维码扫码无效，需移步：站点设置-微信小程序-绑定小程序（本小程序需要已经在小程序官方，发布成功的，才有效）'),
				array('type'=>'copy','name'=>'manageurl','title'=>'掌上管理URL','read'=>true,'help'=>'用于手机后台管理,<red>站点信息跟扫码后进入的不一样</red>？请移步手机端--站点--绑定现有站点，进行<red>站点绑定</red>！'),
				array('type'=>'ewm','name'=>'manageurl2','title'=>'掌上管理','show_wxapp_ewm'=>$setting['cover_wxapp']?false:true,'help'=>'用于手机后台管理'),
				array(
					'type'=>'array',
					//'title'=>'关键字列表',
					'name'=>'keywords',
					'add_tip'=>'添加一个关键词',
					'list'=>array(
						array('type'=>'string','name'=>'keyword','title'=>'关键词内容'),
						array('type'=>'radio','name'=>'type','title'=>'关键词类型','options'=>'keytypes'),
					),'details'=>array(
						array('type'=>'radio','name'=>'type','title'=>'关键词类型','options'=>'keytypes','help'=>'精确匹配：当粉丝输入的内容和本回复规则的关键词一致时触发回复。</br>模糊匹配：当粉丝输入的内容包含本回复规则的关键词时触发回复。</br>正则匹配：使用MySQL的匹配引擎，请使用MySQL的正则语法。</br>正则匹配示例: ^微信匹配以“微信”开头的语句；</br>微信$匹配以“微信”结尾的语句；</br>^微信$匹配等同“微信”的语句；</br>微信匹配包含“微信”的语句；</br>[0-9.-]匹配所有的数字，句号和减号；</br>^[a-zA-Z_]$所有的字母和下划线；</br>^[[:alpha:]]{3}$所有的3个字母的单词；</br>^a{4}$aaaa；^a{2,4}$aa，aaa或aaaa；^a{2,}$匹配多于两个a的字符串。'),
						array('type'=>'string','name'=>'keyword','title'=>'关键词内容','help'=>''),
					)
				),
				array('type'=>'switch','name'=>'status','title'=>'是否启用'),
			)),
			array('title'=>'设置封面参数','list'=>array(
				array('type'=>'string','name'=>'title','title'=>'标题'),
				array('type'=>'text','name'=>'description','title'=>'描述'),
				array('type'=>'image','name'=>'picurl','title'=>'图片','help'=>'建议尺寸：宽640px,高320px'),
			)),
		),
	);
	exi($return);
}
if($_RQ['op']=='welcome'){
	if(!$_RQ['pluginname']){
			exi('请选择模块！','error');
	}
	if($_RQ['pluginname']!='core'){
		$module=pdo_get('core_plugins',array('name'=>$_RQ['pluginname']));
		if(!$module){
			exi('找不到指定模块！'.$_RQ['pluginname'],'error');
		}
	}
	if($module['welcomehdps']){
			$module['welcomehdps']=json_decode($module['welcomehdps'],true);
			$return['iswelcome']=count($module['welcomehdps'])>0?1:0;
	}
	$htmls=array();
	$intypes=get_plugin_intypes();
	if($module['name']=='jy_weishop' || $module['basic']=='jy_weishop'){
			if($return['iswelcome']!=1){
				$module['welcomehdps'] =array(
					array('title'=>'他们都在用小程序微商城','desc'=>'解决开店问题 助力小程序/H5/PC商城一体化','picurl'=>'../core/resource/images/welcomehdp01.jpg'),
					array('title'=>'粉丝变渠道商 激活圈内用户','desc'=>'分销加盟模式，让运营成为一种享受','picurl'=>'../core/resource/images/welcomehdp02.jpg'),
					array('title'=>'各种运营场景体 供你选择','desc'=>'根据自己的运营场景，个性组合搭建','picurl'=>'../core/resource/images/welcomehdp03.jpg'),
					array('title'=>'海量个性营销功能 助力市场激活','desc'=>'活跃，激活，促交面面俱到 让营销不在乏味','picurl'=>'../core/resource/images/welcomehdp04.jpg'),
				);
			}
			$legaos = pdo_getall('core_plugins',array('basic'=>'jy_weishop','is_legao'=>1));
			foreach($legaos as &$item){
				$item['intypetitle']=$intypes[$item['intype']];
			}
			$return['iswelcome']=1;
			$return['legaos']=$legaos;		
	}
	$htmls[]=array('type'=>'welcome');
	$return['welcomehdps']=$module['welcomehdps'];
	$module['intypetitle']=$intypes[$module['intype']];
	$return['plugin']= $module;
	$return['htmls']=$htmls;
	$_SESSION['iswelcome']=1;
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	$postdata['plugin']=$_RQ['plugin'];
	cfc('core')->save_back_news($postdata);
	exi('操作成功！');
}